package com.gy.smart.handle;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.handle.adapter.HandleSelectAdapter;
import com.gy.smart.handle.bean.HandleThemeBean;
import com.gy.smart.util.DeviceInfo;
import com.gy.smart.util.LogUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.Urls;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2017/1/5.
 * Describe:首页选择办事
 */
public class HandleSelectActivity extends BaseActivity {

    private GridView gridView;
    private HandleSelectAdapter selectAdapter;
    private List<HandleThemeBean> datas,requestList,selectList;
    private int[] selects;
    private String theme = "";

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_handle_select);
        setTitleMsg("添加办事主题");
        _baseBack.setImageResource(R.mipmap.ic_left_close);
        _baseRight_text.setText("完成");
        gridView = (GridView) findViewById(R.id.activity_handle_select_gridview);

    }

    @Override
    protected void initData() {
        selectList = (List<HandleThemeBean>) getIntent().getSerializableExtra("list");
        datas = new ArrayList<>();
        requestList = new ArrayList<>();
    }

    @Override
    protected void setListener() {
        super.setListener();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selects[position] = (selects[position] + 1) % 2;
                selectAdapter.setSelect(position,selects[position]);
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        getList();
    }

    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()){
            case R.id._right:
                int select = 0;
                theme = "";
                for (int i = 0; i < selects.length; i++) {
                    if(selects[i] == 1){
                        select++;
                        theme += datas.get(i).theme_id + ",";
                    }
                }
                if(select < 6){
                    showToast("请选择不小于6个主题");
                } else {
                    theme = theme.substring(0,theme.length() - 1);
                    LogUtil.e(theme+"----");
                    updateData();
                }
                break;
        }
    }

    private void getList(){
        showLoadDialog(1);
        RequestParams params = new RequestParams(Urls.url_handle_theme);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<HandleThemeBean>>(){}.getType();
                    requestList = gson.fromJson(result.data,type);
                    datas.addAll(requestList);

                    selects = new int[datas.size()];
                    for (int i = 0; i < datas.size(); i++) {
                        for (int j = 0; j < selectList.size(); j++) {
                            if(datas.get(i).theme_id == selectList.get(j).theme_id){
                                selects[i] = 1;
                            }
                        }
                    }
                    selectAdapter = new HandleSelectAdapter(HandleSelectActivity.this,datas,selects);
                    gridView.setAdapter(selectAdapter);
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }

    private void updateData(){
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.url_change_handle);
        params.addParameter("userid", DeviceInfo.getIMEI(this));
        params.addParameter("theme",theme);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    showToast("更新成功");
                    sendBroadcast(new Intent("refresh_home"));
                    finish();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
